package fdoj.eventInvitations.servlet;

import java.io.IOException;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import fdoj.eventInvitations.dao.PersonDao;
import fdoj.eventInvitations.entity.Person;

public class CompleteRegController extends HttpServlet{
	@EJB
	private PersonDao personDao;


	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
			doGet(request, response);
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			HttpSession session=request.getSession(true);
			Person loggedUser=null;
			Person p = personDao.findById(Integer.parseInt(request.getParameter("id")));
			if(p.getRegToken().equals(request.getParameter("regToken"))){
				p.setRegToken("true");
				personDao.merge(p);
				request.getSession().setAttribute("loggedUser", p);
				response.sendRedirect(response.encodeRedirectURL("./home"));
				return;
			}else{
				response.sendRedirect(response.encodeRedirectURL("./index"));
				return;
			}
		} catch (Exception e) {
			e.printStackTrace();

		}
		response.sendRedirect(response.encodeRedirectURL("./error.jsp"));

	}
}
